var app = getApp()
Page({
  data: {
    db: {},
    about: "none",
    imageBase64: [],
    schoolId: "未填写",
    call: "未填写",
    page: 1,
    show: false,
    buttonshow: true,
    url: '', //头像
    nicheng: '', //昵称
    url1: '',
    nicheng1: ''
  },
  dianhua(){
    wx.navigateTo({
      url: '',
    })
  },
  guanbi() {
    this.setData({
      show: false
    })
  },
  shuru(e) {  //输入昵称
    console.log(e.detail.value)
    this.setData({
      nicheng: e.detail.value
    })
  },
  onChooseAvatar(e) {  //选择头像
    const {
      avatarUrl
    } = e.detail
    this.setData({
      url: avatarUrl
    })
  },

  onShow: function () {
    var e = this;
    wx.cloud.database().collection("userInformation").where({
      _openid: app.globalData.openid
    }).get({
      success: function (a) {
        e.setData({
          call: a.data[0].call,
          schoolId: a.data[0].schoolId
     
        });
      }
    })
  },
  getUserProfile() {
    this.setData({
      show: true,
      buttonshow:false
    })
  },
  queren() {
    var that = this
    if (this.data.url == '') {
      wx.showToast({
        title: "头像为空",
        icon: "none",
        image: "",
        duration: 1500,
        mask: !1
      });
      return
    }
    if (this.data.nicheng == '') {
      wx.showToast({
        title: "昵称为空",
        icon: "none",
        image: "",
        duration: 1500,
        mask: !1
      });
      return
    }
    
    //照片存到云
    var e = 1e6 * Math.random(),
      a = this.data.url,
      i = e + a.match(/\.[^.]+?$/)[0];
    console.log("url为", this.data.url)
    wx.cloud.uploadFile({
      cloudPath: "touxiang/" + i,
      filePath: a,
      success: function (e) {
        var b = e.fileID;
        const db = wx.cloud.database({
          env: "duasss-8go24puka422939f"
        });
        db.collection("userInformation").where({
          _openid: app.globalData.openid
        }).get({
          success: function (a) {
            if (0 == a.data.length) {
              db.collection("userInformation").add({
                data: {
                  time: Date.now(),
                  nickName: that.data.nicheng,
                  avatarUrl: b,
                }
              })
              app.globalData.nicheng = that.data.nicheng
              app.globalData.url = b
            }else{
              this.onLoad()
              return
            }
          }
        })
      },
      fail: function (e) {
        console.log("失败", e)
      }
    })

    this.setData({
      url1: this.data.url,
      nicheng1: this.data.nicheng,
      show: false
    })

  },
  onPullDownRefresh: function () {
   // this.onLoad();
    this.onShow();
  },
  onLoad() {
    this.setData({
      url1: app.globalData.url,
      nicheng1: app.globalData.nicheng
    });
  },
  showPop() {
    if (this.data.about === "none") {
      this.setData({
        about: "block"
      })
    } else {
      this.setData({
        about: "none"
      })
    }
  },

})